{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import matplotlib.font_manager as fm  # 导入字体管理器模块并别名为 fm\n",
    "from matplotlib.font_manager import FontProperties"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "选定TTF文件配置字体"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 设置字体路径,注意这里使用你系统中 Noto Sans CJK SC 的实际路径\n",
    "font_path = '/usr/share/fonts/opentype/noto/NotoSansCJK-Regular.ttc'\n",
    "# load and config fonts\n",
    "prop = fm.FontProperties(fname=font_path)\n",
    "plt.rcParams['font.family'] = prop.get_name()\n",
    "plt.rcParams['axes.unicode_minus'] = False      # 显示负号"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用系统默认字体"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['C:\\\\Windows\\\\Fonts\\\\simhei.ttf']\n"
     ]
    }
   ],
   "source": [
    "# 获取系统中所有可用字体\n",
    "font_path = fm.findSystemFonts()\n",
    "chinese_fonts = [f for f in font_path if 'simhei' in f.lower() or 'heiti' in f.lower() or 'microsoftyahei' in f.lower()]\n",
    "print(chinese_fonts)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 取第一个字体使用\n",
    "chinese_font = chinese_fonts[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'C:\\\\Windows\\\\Fonts\\\\simhei.ttf'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chinese_font"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "font = FontProperties(fname=chinese_font) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "字体family: SimHei\n"
     ]
    }
   ],
   "source": [
    "# 获取字体family\n",
    "font_family = font.get_name()\n",
    "print(f\"字体family: {font_family}\") "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "自动取字体方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "def set_chinese_font(plt: \"matplotlib.pyplot\") -> None:\n",
    "    \"\"\"\n",
    "        设置中文字体\n",
    "    \"\"\"\n",
    "    import matplotlib.font_manager as fm  # 导入字体管理器模块并别名为 fm\n",
    "    from matplotlib.font_manager import FontProperties\n",
    "    # 获取系统中所有可用字体\n",
    "    font_path = fm.findSystemFonts()\n",
    "    chinese_fonts = [f for f in font_path if 'simhei' in f.lower() or 'heiti' in f.lower() or 'microsoftyahei' in f.lower()]\n",
    "    chinese_font = chinese_fonts[0]\n",
    "    font = FontProperties(fname=chinese_font) \n",
    "    font_family = font.get_name()\n",
    "    plt.rcParams[\"font.family\"] = font_family"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "my_env",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
